package com.irdstudio.efp.esb.api.service.wsd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.CurrencyConstant;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.framework.core.util.ValidationUtils;
import com.irdstudio.basic.framework.redis.redisutil.RedisUtil;
import com.irdstudio.basic.framework.redis.redisutil.keys.Ocm_Nls_Key;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.finalcredit.NlsWsdExtInfo;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.finalcredit.NlsWsdFinalReqBean;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.fristCredit.NlsWsdFirstExtInfo;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.fristCredit.NlsWsdFirstReqBean;
import com.irdstudio.efp.esb.api.bean.wsd.credit.resp.NlsWsdFirstRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.InstitutionEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.req.EsbReqSysHeadBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.service.bo.req.wsd.firstnotice.FirstApproveNoticeReqBean;
import com.irdstudio.efp.esb.service.bo.req.wsd.riskmanagement.RiskManagementApproveReqBean;
import com.irdstudio.efp.esb.service.facade.EsbInvokeWrapper;
import com.irdstudio.efp.esb.service.facade.EsbInvokerWorker;
import com.irdstudio.efp.esb.service.facade.wsd.firstnotice.FirstApproveNoticeService;
import com.irdstudio.efp.esb.service.facade.wsd.riskmanagement.RiskManagementApproveService;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;
import com.irdstudio.efp.nls.common.constant.QueueTaskEnums;
import com.irdstudio.efp.nls.service.facade.NlsPublicService;
import com.irdstudio.efp.nls.service.facade.NlsQueueSoltComnService;
import com.irdstudio.efp.nls.service.facade.WsApplyNotifyService;
import com.irdstudio.efp.nls.service.facade.WsApproveackNotifyService;
import com.irdstudio.efp.nls.service.facade.queue.NlsProcessBizService;
import com.irdstudio.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.irdstudio.efp.nls.service.vo.WsApplyNotifyVO;
import com.irdstudio.efp.nls.service.vo.WsApproveackNotifyVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.irdstudio.basic.framework.redis.redisutil.keys.Ocm_Nls_Key.ESBRESPSERVICEBEAN_KEY;
import static com.irdstudio.efp.console.common.ConsoleConstant.NLSCREDIT_DEALING;
import static com.irdstudio.efp.limit.common.LimitConstant.CRD_LMT_TYPE_10;

/**
 * 需求编号:
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/30
 * 功能描述:
 * 修改日期:2019/7/30
 * 修改描述:
 */
@Service("nlsWsdProcessServcie")
public class NlsWsdProcessServcie implements FrameworkService {

    /**
     * 需求编号:2019D0519
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:授信初审申请处理流程
     * 修改日期:2019/8/8
     * 修改描述:
     */
    public EsbRespServiceBean processFirstCreditApply(NlsWsdFirstReqBean reqWsdFirstCreditBean, EsbReqServiceBean esbReqServiceBean) throws ESBException {
        try {
            validateReqInfo(reqWsdFirstCreditBean);

            // 校验是否有对应的产品信息
            PrdInfoVO proInfo = nlsPublicService.getProInfo(reqWsdFirstCreditBean.getProdCd());
            if (proInfo == null) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+" 产品信息校验失败,获取不到该产品信息!!!");
                throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品信息校验失败!!!");
            }
            String sysInfoOpenDay = nlsPublicService.getSysInfoOpenDay(BaseConstant.SYS_ID_1001);

            String glblSrlNo = Optional.ofNullable(esbReqServiceBean.getSysHead()).orElseGet(EsbReqSysHeadBean::new).getGlblSrlNo();

            // 授信初审信息表落库 【授信初审信息落表】【授信申请信息】 【网贷业务过程信息】
            composeAndInsertFirstCreditInfo(reqWsdFirstCreditBean, proInfo, sysInfoOpenDay,glblSrlNo);
            // 回掉网商贷接口 授信初审成功
            invokeFirstApprNotice(reqWsdFirstCreditBean);

            NlsWsdFirstRespBean successRespBean = getSuccessRespBean(reqWsdFirstCreditBean.getRqsIdmptntID(), reqWsdFirstCreditBean.getAdptCrdtAplNo());
            // 授信resInfo落表
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean, successRespBean);
            updateWsApprovNotify(reqWsdFirstCreditBean, successRespBean);
            // 将审批结果放入redis中
            setEsbRespServiceBean(reqWsdFirstCreditBean.getRqsIdmptntID() + "_" + reqWsdFirstCreditBean.getAdptCrdtAplNo(), esbRespServiceBean);
            // 清除否则影响后续流程
            TraceUtil.clear();
            return esbRespServiceBean;
        } catch (ESBException e) {
            throw new ESBException(e.getErrorCode(), e.getMessage());
        }

    }

    /**
     * 需求编号:2019D0910
     * 问题编号:复审验证信息
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:
     * 修改日期:2019/8/8
     * 修改描述:
     */
    public EsbRespServiceBean processFinalCreditApply(NlsWsdFinalReqBean reqWsdCreditBean, EsbReqServiceBean esbReqServiceBean) throws ESBException {
        try {
            // 校验必要的信息
            validateFinlaReqInfo(reqWsdCreditBean);
            // 插入和更新终审授信信息
            NlsCreditInfoVO nlsCreditInfoVO = composeAndUpdateFianlCreditInfo(reqWsdCreditBean);
            //调用大数据风控接口进行审批
            invokeRiskApprove(reqWsdCreditBean, nlsCreditInfoVO);
            // 插入返回信息
            NlsWsdFirstRespBean successRespBean = getSuccessRespBean(reqWsdCreditBean.getRqsIdmptntID(), reqWsdCreditBean.getAdptCrdtAplNo());
            // 更新审批信息
            updateWsApproveackNotify(reqWsdCreditBean, successRespBean);
            // 清除否则影响后续流程
            TraceUtil.clear();
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean, successRespBean);
            setEsbRespServiceBean(reqWsdCreditBean.getRqsIdmptntID() + "_" + reqWsdCreditBean.getAdptCrdtAplNo(), esbRespServiceBean);
            return esbRespServiceBean;
        } catch (ESBException e) {
            throw new ESBException(e.getErrorCode(), e.getMessage(), e);
        }
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/23
     * 功能描述:校验复审审批信息
     * 修改日期:2019/8/23
     * 修改描述:
     */
    private void validateFinlaReqInfo(NlsWsdFinalReqBean reqWsdCreditBean) throws ESBException {
        String pdNm = reqWsdCreditBean.getPdNm();
        if (!"网商联合贷".equals(pdNm)) {
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 产品名称不正确!!!");
            throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品名称不正确!!!");
        }
        try {
            // 身份证校验
            MessageRuleCheckUtil.validateIdNo18(reqWsdCreditBean.getCertNo(), EsbRetCodeStandard.CPXXJYBTG.getValue());
        } catch (BizException e) {
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 证件号码格式错误!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "证件号码格式错误!!!");
        }
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/23
     * 功能描述:校验必要的信息
     * 修改日期:2019/8/23
     * 修改描述:
     */
    private void validateReqInfo(NlsWsdFirstReqBean reqWsdFirstCreditBean) throws ESBException {
        // 产品名称校验
        String pdNm = reqWsdFirstCreditBean.getPdNm();
        if (!"网商联合贷".equals(pdNm)) {
            logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+" 产品信息校验失败,产品名称不为网商贷!!!");
            throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品信息校验失败!!!");
        }
        try {
            // 身份证校验
            MessageRuleCheckUtil.validateIdNo18(reqWsdFirstCreditBean.getCertNo(), EsbRetCodeStandard.CPXXJYBTG.getValue());
            // 手机号校验
            boolean flag = false;
            try {
                NlsWsdFirstExtInfo nlsWsdFirstExtInfo = reqWsdFirstCreditBean.getNlsWsdFirstExtInfo();
                String mobile = nlsWsdFirstExtInfo.getMOBILE();
                flag = ValidationUtils.validPhoneNum("0", mobile);
            } catch (Exception e) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+" 附加信息格式错误,获取不到客户手机号!!!");
                throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "附加信息格式错误,获取不到客户手机号!!!");
            }
            if (!flag) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+" 附加信息格式错误,手机号码格式错误!!!");
                throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "手机号码格式错误!!!");
            }
        } catch (BizException e) {
            logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+" 附加信息格式错误,证件号码格式错误!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "证件号码格式错误!!!");
        }

    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:更新初审审批信息
     * 修改日期:2019/8/19
     * 修改描述:
     */
    private void updateWsApprovNotify(NlsWsdFirstReqBean reqWsdFirstCreditBean, NlsWsdFirstRespBean successRespBean) throws ESBException {
        WsApplyNotifyVO updateWsApproveackNotifyVO = new WsApplyNotifyVO();
        updateWsApproveackNotifyVO.setRequestId(reqWsdFirstCreditBean.getRqsIdmptntID());
        updateWsApproveackNotifyVO.setResultInfo(JSON.toJSONString(successRespBean));
        int result = wsApplyNotifyService.updateByPk(updateWsApproveackNotifyVO);
        if (result != 1) {
            logger.error("授信初审【REQUESTID】:+"+reqWsdFirstCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdFirstCreditBean.getAdptCrdtAplNo()+"更新初审信息表异常!!!");
            throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新初审信息表异常!!!");
        }

    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:调用初审审批通知接口
     * 修改日期:2019/8/19
     * 修改描述:
     */
    private void invokeFirstApprNotice(NlsWsdFirstReqBean reqWsdFirstCreditBean) {
        FirstApproveNoticeReqBean firstApproveNoticeReqBean = new FirstApproveNoticeReqBean();
        firstApproveNoticeReqBean.setRqsIdmptntID(reqWsdFirstCreditBean.getRqsIdmptntID());
        firstApproveNoticeReqBean.setAdptCrdtAplNo(reqWsdFirstCreditBean.getAdptCrdtAplNo());
        firstApproveNoticeReqBean.setCertNm(reqWsdFirstCreditBean.getCertNm());
        firstApproveNoticeReqBean.setCertNo(reqWsdFirstCreditBean.getCertNo());
        firstApproveNoticeReqBean.setNtwrkMrchDataTpVal("COOPERATION_BANK_APPROVE_INFO");
        firstApproveNoticeReqBean.setExnFld("{}");
        EsbInvokeWrapper esbInvokeWrapper = new EsbInvokeWrapper(firstApproveNoticeService, firstApproveNoticeReqBean, nlsQueueSoltComnService);
        EsbInvokerWorker esbInvokerWorker = new EsbInvokerWorker(esbInvokeWrapper);
        threadPools.submit(esbInvokerWorker);

    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:获取成功交易返回Bean
     * 修改日期:2019/8/19
     * 修改描述:
     */
    public NlsWsdFirstRespBean getSuccessRespBean(String requestId, String applyNo) {
        NlsWsdFirstRespBean nlsWsdFirstRespBean = new NlsWsdFirstRespBean();
        nlsWsdFirstRespBean.setRetCd(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE());
        nlsWsdFirstRespBean.setRetMsg("交易成功");
        nlsWsdFirstRespBean.setRqsIdmptntID(requestId);
        nlsWsdFirstRespBean.setAdptCrdtAplNo(applyNo);
        nlsWsdFirstRespBean.setRtryFlg("N");
        return nlsWsdFirstRespBean;
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:获取失败交易返回Bean
     * 修改日期:2019/8/19
     * 修改描述:
     */
    public NlsWsdFirstRespBean getFailureRespBean(String requestId, String applyNo, String code, String msg) {
        NlsWsdFirstRespBean nlsWsdFirstRespBean = new NlsWsdFirstRespBean();
        nlsWsdFirstRespBean.setRetCd(code);
        nlsWsdFirstRespBean.setRetMsg(msg);
        nlsWsdFirstRespBean.setRqsIdmptntID(requestId);
        nlsWsdFirstRespBean.setAdptCrdtAplNo(applyNo);
        nlsWsdFirstRespBean.setRtryFlg(EsbRetCodeStandard.YWLJCLYC.getValue().equals(code)?"Y":"N");
        return nlsWsdFirstRespBean;
    }

    /**
     * 需求编号:2019D0519
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:授信初审信息落库
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private void composeAndInsertFirstCreditInfo(NlsWsdFirstReqBean reqWsdCreditBean, PrdInfoVO prdInfoVO, String sysInfoOpenDay,String globaNo) throws ESBException {
        // 插入授信申请信息表
        insertNlsCreditInfo(reqWsdCreditBean, prdInfoVO, sysInfoOpenDay,globaNo);
        // 插入网贷业务过程表
        insertNlsProcessInfo(reqWsdCreditBean);
        // 插入授信初审信息表
        insertApplyNotify(reqWsdCreditBean);
    }

    /**
     * 需求编号:2019D0519
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:插入网贷业务信息
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private void insertNlsProcessInfo(NlsWsdFirstReqBean reqWsdCreditBean) throws ESBException {
        // 插入网贷业务过程表 保证后续记录各插槽执行状态
        NlsProcessBizVO nlsProcessBizVo = new NlsProcessBizVO();
        // 来源授信申请接口【必输】 授信申请流水号
        nlsProcessBizVo.setApplySeq(reqWsdCreditBean.getAdptCrdtAplNo());
        // 来源授信申请接口【必输】 客户姓名
        nlsProcessBizVo.setCusName(reqWsdCreditBean.getCertNm());
        // 来源授信申请接口【必输】 产品ID
        nlsProcessBizVo.setPrdId(reqWsdCreditBean.getProdCd());
        // 来源授信申请接口【必输】 产品编码与产品ID一样
        nlsProcessBizVo.setPrdCode(reqWsdCreditBean.getProdCd());
        // 来源授信申请接口【必输】 产品名称
        nlsProcessBizVo.setPrdName(reqWsdCreditBean.getPdNm());
        // 默认赋值 挂起次数
        nlsProcessBizVo.setHangTimes(QueueTaskEnums.QueueHangTimesEnum.INITIALIZE.getTimes());
        // 默认赋值 队列任务状态
        nlsProcessBizVo.setQueueTaskState(QueueTaskEnums.QueueTaskStateEnum.EXECUTORY.getCode());
        // add by ludayong 2019/11/15 【数据权限】增加经办机构赋值
        // 经办机构
        nlsProcessBizVo.setNlsOperOrgid(InstitutionEnums.InstitutionEnum.GZYHGZFHPHJRB.getCode());
        // 默认赋值信息
        setServerInsertDefaultProperty(nlsProcessBizVo);
        NlsProcessBizVO isHasNlsProcess = new NlsProcessBizVO();
        isHasNlsProcess.setApplySeq(reqWsdCreditBean.getAdptCrdtAplNo());
        isHasNlsProcess = nlsProcessBizService.queryByPk(isHasNlsProcess);
        if (isHasNlsProcess == null){
            int result = nlsProcessBizService.insertNlsProcessBiz(nlsProcessBizVo);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"插入网贷业务过程表异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "插入网贷业务过程表异常!!!");
            }
        }else {
            setServerUpdateDefaultProperty(isHasNlsProcess);
            int result = nlsProcessBizService.updateByPk(isHasNlsProcess);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"更新网贷业务过程表异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新网贷业务过程表异常!!!");
            }
        }
    }

    /**
     * 需求编号:2019D0519
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:插入授信初审信息
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private void insertApplyNotify(NlsWsdFirstReqBean reqWsdCreditBean) throws ESBException {
        // 插入授信初审信息表
        NlsWsdFirstExtInfo nlsWsdFirstExtInfo = reqWsdCreditBean.getNlsWsdFirstExtInfo();
        WsApplyNotifyVO wsApplyNotifyVO = new WsApplyNotifyVO();
        wsApplyNotifyVO.setRequestId(reqWsdCreditBean.getRqsIdmptntID());
        wsApplyNotifyVO.setApplyNo(reqWsdCreditBean.getAdptCrdtAplNo());
        wsApplyNotifyVO.setCertType(reqWsdCreditBean.getCertTp());
        wsApplyNotifyVO.setCertName(reqWsdCreditBean.getCertNm());
        wsApplyNotifyVO.setCertNo(reqWsdCreditBean.getCertNo());
        wsApplyNotifyVO.setBusinessModel(reqWsdCreditBean.getNtwrkMrchLoanPdVal());
        wsApplyNotifyVO.setMobile(nlsWsdFirstExtInfo.getMOBILE());
        wsApplyNotifyVO.setAddress(getAddressFromJson(nlsWsdFirstExtInfo.getADDRESS()));
        wsApplyNotifyVO.setCertValidEndDate(nlsWsdFirstExtInfo.getCERT_VALID_END_DATE());
        wsApplyNotifyVO.setBusDataReqDate(nlsWsdFirstExtInfo.getBUSDATA_REQ_DATE());
        wsApplyNotifyVO.setBusInfoExistFlag(nlsWsdFirstExtInfo.getBUS_INFO_EXIST_FLAG());
        wsApplyNotifyVO.setNotExistReason(nlsWsdFirstExtInfo.getNOT_EXIST_REASON());
        wsApplyNotifyVO.setCompanyInfoName(nlsWsdFirstExtInfo.getCOMPANY_INFO_NAME());
        wsApplyNotifyVO.setCompanyInfoLawer(nlsWsdFirstExtInfo.getCOMPANY_INFO_LAWER());
        wsApplyNotifyVO.setRegisterNo(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_NO());
        wsApplyNotifyVO.setRegisterDate(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_DATE());
        wsApplyNotifyVO.setRegisterAddress(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_ADDRESS());
        wsApplyNotifyVO.setRegisterAddressAreaCode(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_ADDRESS_AREA_CODE());
        wsApplyNotifyVO.setRegisterAddressArea(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_ADDRESS_AREA());
        wsApplyNotifyVO.setRegisterFund(String.valueOf(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_FUND()));
        wsApplyNotifyVO.setFundCurrency(nlsWsdFirstExtInfo.getCOMPANY_INFO_FUND_CURRENCY());
        wsApplyNotifyVO.setTradeCode(nlsWsdFirstExtInfo.getCOMPANY_INFO_TRADE_CODE());
        wsApplyNotifyVO.setManageRange(nlsWsdFirstExtInfo.getCOMPANY_INFO_MANAGE_RANGE());
        wsApplyNotifyVO.setOrgCode(nlsWsdFirstExtInfo.getCOMPANY_INFO_ORG_CODE());
        wsApplyNotifyVO.setRegisterDepartment(nlsWsdFirstExtInfo.getCOMPANY_INFO_REGISTER_DEPARTMENT());
        wsApplyNotifyVO.setStatusId(nlsWsdFirstExtInfo.getCOMPANY_INFO_STATUS_ID());
        wsApplyNotifyVO.setStatusDesc(nlsWsdFirstExtInfo.getCOMPANY_INFO_STATUS_DESC());
        wsApplyNotifyVO.setLastCheckYear(nlsWsdFirstExtInfo.getCOMPANY_INFO_LAST_CHECK_YEAR());
        wsApplyNotifyVO.setManageBeginDate(nlsWsdFirstExtInfo.getCOMPANY_INFO_MANAGE_BEGIN_DATE());
        wsApplyNotifyVO.setManageEndDate(nlsWsdFirstExtInfo.getCOMPANY_INFO_MANAGE_END_DATE());
        wsApplyNotifyVO.setOpenDate(nlsWsdFirstExtInfo.getCOMPANY_INFO_OPEN_DATE());
        wsApplyNotifyVO.setCompanyType(nlsWsdFirstExtInfo.getCOMPANY_INFO_COMPANY_TYPE());
        wsApplyNotifyVO.setEconomicType(nlsWsdFirstExtInfo.getCOMPANY_INFO_ECONOMIC_TYPE());
        wsApplyNotifyVO.setTargetJyFlag1(nlsWsdFirstExtInfo.getTARGET_JY_FLAG_1());
        wsApplyNotifyVO.setIndustryName(nlsWsdFirstExtInfo.getINDUSTRY_NAME());
        wsApplyNotifyVO.setInputDate(DateUtility.getCurrAppDateTimeStr());
        wsApplyNotifyVO.setCreateUser(BaseConstant.ADMIN);
        wsApplyNotifyVO.setCreateTime(new Date());
        wsApplyNotifyVO.setUpdateUser(BaseConstant.ADMIN);
        wsApplyNotifyVO.setUpdateTime(new Date());

        WsApplyNotifyVO isHasWsApplyNotifyVO = wsApplyNotifyService.queryByPk(wsApplyNotifyVO);
        if (isHasWsApplyNotifyVO == null){
            int result = wsApplyNotifyService.insertWsApplyNotify(wsApplyNotifyVO);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"插入初审信息表出现异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "插入初审信息表出现异常!!!");
            }
        }else {
            isHasWsApplyNotifyVO.setCreateUser(BaseConstant.ADMIN);
            isHasWsApplyNotifyVO.setCreateTime(new Date());
            isHasWsApplyNotifyVO.setUpdateUser(BaseConstant.ADMIN);
            isHasWsApplyNotifyVO.setUpdateTime(new Date());
            int result = wsApplyNotifyService.updateByPk(isHasWsApplyNotifyVO);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"更新初审信息表出现异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新初审信息表出现异常!!!");
            }
        }
        // 将初审信息放入redis
        redisUtil.set(Ocm_Nls_Key.WS_APPLY_NOTIFY_KEY.getPrefix() + reqWsdCreditBean.getAdptCrdtAplNo(), JSON.toJSONString(wsApplyNotifyVO));

    }

    /**
     * 需求编号:2019D0519
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:插入授信申请基本信息
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private void insertNlsCreditInfo(NlsWsdFirstReqBean reqWsdCreditBean, PrdInfoVO prdInfoVO,  String sysInfoOpenDay,String globaNo) throws ESBException {
        // 授信初审ExtInfo
        NlsWsdFirstExtInfo nlsWsdFirstExtInfo = Optional.ofNullable(reqWsdCreditBean.getNlsWsdFirstExtInfo()).orElseGet(NlsWsdFirstExtInfo::new);
        // 插入授信基本信息表
        NlsCreditInfoVO nlsCreditInfoVO = new NlsCreditInfoVO();
        // 来源【网贷初审接口】 授信申请流水号
        nlsCreditInfoVO.setLmtApplySeq(reqWsdCreditBean.getAdptCrdtAplNo());
        // 来源【网贷初审接口】 客户姓名
        nlsCreditInfoVO.setCusName(reqWsdCreditBean.getCertNm());
        // 来源【网贷初审接口】 证件类型
        nlsCreditInfoVO.setCertType(reqWsdCreditBean.getCertTp());
        // 来源【网贷初审接口】 证件号码
        nlsCreditInfoVO.setCertCode(reqWsdCreditBean.getCertNo());
        // 来源【网贷初审接口】 证件有效期
        nlsCreditInfoVO.setCertEndDt(nlsWsdFirstExtInfo.getCERT_VALID_END_DATE());
        // 来源【网贷初审接口】 联系地址
        nlsCreditInfoVO.setIndivRsdAddr(getAddressFromJson(nlsWsdFirstExtInfo.getADDRESS()));
        // 来源【网贷初审接口】 产品ID
        nlsCreditInfoVO.setPrdId(reqWsdCreditBean.getProdCd());
        // 来源【网贷初审接口】 产品代码
        nlsCreditInfoVO.setPrdCode(reqWsdCreditBean.getProdCd());
        // 来源【网贷初审接口】 产品名称
        nlsCreditInfoVO.setPrdName(reqWsdCreditBean.getPdNm());
        // 来源【网贷初审接口】 手机号
        nlsCreditInfoVO.setIndivMobile(nlsWsdFirstExtInfo.getMOBILE());
        // 产品表
        // 【来源产品表】 额度类型
        nlsCreditInfoVO.setLmtType(CRD_LMT_TYPE_10);
        // 【来源产品表】  是否循环
        nlsCreditInfoVO.setCyclicFlg(prdInfoVO.getCyclicWay());
        // 【来源产品表】  申请期限
        nlsCreditInfoVO.setApplyTerm(prdInfoVO.getCreditTerm());
        // 【来源产品表】  期限类型
        nlsCreditInfoVO.setTermType(prdInfoVO.getCreditTermUnit());
        // 【来源产品表】  还款方式 等额本息
        nlsCreditInfoVO.setRepaymentMode(prdInfoVO.getRepayWay());
        // 【来源产品表】  贷款用途 子典项 这里还没有 默认 【消费支出】
        nlsCreditInfoVO.setLoanUseType(prdInfoVO.getLoanUseType());
        // 【来源产品表】  网贷经办机构
        nlsCreditInfoVO.setNlsOperOrgid(prdInfoVO.getOperateAgency());
        // 【来源默认赋值】 审批审批状态 处理中 ConsoleConstant.APRV_STATUS_03)
        nlsCreditInfoVO.setApprvSts(NLSCREDIT_DEALING);
        // 【来源默认赋值】 性别
        nlsCreditInfoVO.setGender(ValidationUtils.getSexFromIdNumber(reqWsdCreditBean.getCertNo()).getCnname());
        // 【来源默认赋值】 申请日期 console库中 pub_sys_info openday
        nlsCreditInfoVO.setApplyDate(sysInfoOpenDay);
        // 【来源默认赋值】 审批日期 console库中 pub_sys_info openday
        nlsCreditInfoVO.setApprvDate(sysInfoOpenDay);
        // 【来源默认赋值】 担保方式 抵押（抵押出去，还有使用权），质押（没有使用权）,保证（担保人），信用（风险最高）
        nlsCreditInfoVO.setAssureMeansMain(BaseConstant.ASSURE_MEANS_00);
        // 【来源默认赋值】 币种 CNY人民币caoxin:这里是否需要转换
        nlsCreditInfoVO.setCurrencyType(CurrencyConstant.CNY);
        // 【来源默认赋值】 渠道名称
        nlsCreditInfoVO.setChannelNo(prdInfoVO.getPrdType());
        // 【来源默认赋值】 全局流水号
        nlsCreditInfoVO.setGlobalSerno(globaNo);
        // 设置默认信息
        setServerInsertDefaultProperty(nlsCreditInfoVO);
        NlsCreditInfoVO isHasNlsCreditInfoVo = nlsCreditInfoService.queryByLmtApplySeq(reqWsdCreditBean.getAdptCrdtAplNo());
        if (isHasNlsCreditInfoVo == null){
            int result = nlsCreditInfoService.insertNlsCreditInfo(nlsCreditInfoVO);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"插入授信申请信息表出现异常!!!!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "插入授信申请信息表出现异常!!!");
            }
        }else {
            setServerUpdateDefaultProperty(nlsCreditInfoVO);
            int result = nlsCreditInfoService.updateByPk(isHasNlsCreditInfoVo);
            if (result != 1) {
                logger.error("授信初审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+"更新授信申请信息表出现异常!!!!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新授信申请信息表出现异常!!!");
            }
        }
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/29
     * 功能描述:从json中获取对象
     * 修改日期:2019/8/29
     * 修改描述:
     */
    private String getAddressFromJson(String addressJson) {
        if (StringUtils.isNotEmpty(addressJson)) {
            JSONObject jsonObject = JSONObject.parseObject(addressJson);
            String prov = (String) Optional.ofNullable(jsonObject.get("prov")).orElse("");
            String city = (String) Optional.ofNullable(jsonObject.get("city")).orElse("");
            String area = (String) Optional.ofNullable(jsonObject.get("area")).orElse("");
            String address = (String) Optional.ofNullable(jsonObject.get("address")).orElse("");
            return prov + city + area + address;
        }
        return StringUtils.EMPTY;
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:更新复审审批结果
     * 修改日期:2019/8/19
     * 修改描述:
     */
    private void updateWsApproveackNotify(NlsWsdFinalReqBean reqWsdCreditBean, NlsWsdFirstRespBean successRespBean) throws ESBException {
        WsApproveackNotifyVO updateWsApproveackNotifyVO = new WsApproveackNotifyVO();
        updateWsApproveackNotifyVO.setRequestId(reqWsdCreditBean.getRqsIdmptntID());
        updateWsApproveackNotifyVO.setResultInfo(JSON.toJSONString(successRespBean));
        int result = wsApproveackNotifyService.updateByPk(updateWsApproveackNotifyVO);
        if (result != 1) {
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 更新复审信息失败!!!");
            throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新复审信息失败!!!");
        }
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:调用风控审批接口
     * 修改日期:2019/8/19
     * 修改描述:
     */
    private void invokeRiskApprove(NlsWsdFinalReqBean reqWsdCreditBean, NlsCreditInfoVO nlsCreditInfoVO) {
        // 获取复审附加信息
        NlsWsdExtInfo nlsWsdExtInfo = Optional.ofNullable(reqWsdCreditBean.getNlsWsdExtInfo()).orElseGet(NlsWsdExtInfo::new);
        NlsWsdExtInfo.PERSONALCREDITREPORTBean personalCreditReport = Optional.ofNullable(nlsWsdExtInfo.getPERSONAL_CREDIT_REPORT()).orElseGet(NlsWsdExtInfo.PERSONALCREDITREPORTBean::new);
        NlsWsdExtInfo.PERSONALCREDITREPORTBean.CreditResumBean creditResum = Optional.ofNullable(personalCreditReport.getCreditResum()).orElseGet(NlsWsdExtInfo.PERSONALCREDITREPORTBean.CreditResumBean::new);
        // 从redis获取初审数据
        WsApplyNotifyVO wsApplyNotify = nlsPublicService.getWsApplyNotify(reqWsdCreditBean.getAdptCrdtAplNo());

        // 封装复审审批接口信息
        RiskManagementApproveReqBean riskManagementApproveReqBean = new RiskManagementApproveReqBean();
        // 基本信息数组
        RiskManagementApproveReqBean.QryRptsInfArry qryRptsInfArry = new RiskManagementApproveReqBean.QryRptsInfArry();
        // 征信信息数组
        RiskManagementApproveReqBean.QryRptsInfArry.ExtGuarDtlInf extGuarDtlInf = new RiskManagementApproveReqBean.QryRptsInfArry.ExtGuarDtlInf();
        // 对外担保信息
        RiskManagementApproveReqBean.QryRptsInfArry.ExtGuarDtlInf.CrdtInfo crdtInfo = new RiskManagementApproveReqBean.QryRptsInfArry.ExtGuarDtlInf.CrdtInfo();
        // 基本数据
        qryRptsInfArry.setRqsIdmptntID(reqWsdCreditBean.getRqsIdmptntID());
        qryRptsInfArry.setAplNo(reqWsdCreditBean.getAdptCrdtAplNo());
        qryRptsInfArry.setCertNo(reqWsdCreditBean.getCertNo());
        qryRptsInfArry.setCertTp(reqWsdCreditBean.getCertTp());
        qryRptsInfArry.setCustNm(reqWsdCreditBean.getCertNm());
        qryRptsInfArry.setPassFlg(reqWsdCreditBean.getPassFlg());
        qryRptsInfArry.setCrdtRcmndnAmt(reqWsdCreditBean.getCrdtRcmndnAmt());
        qryRptsInfArry.setCrdtAnulIntRtLmt(reqWsdCreditBean.getCrdtAnulIntRtLmt());
        qryRptsInfArry.setCrdtAnulIntRtLwrLmt(reqWsdCreditBean.getCrdtAnulIntRtLwrLmt());
        qryRptsInfArry.setNtwrkMrchLoanPdVal(reqWsdCreditBean.getNtwrkMrchLoanPdVal());
        // 提额标志 提额标志 1:已经提额，0:未提额;是否有两个条或者以上有效的额度信息
        qryRptsInfArry.setUpgdLmtFlg(lmtPrdContService.countLmtProContByCertTypeAndCertNo(reqWsdCreditBean.getCertTp(), reqWsdCreditBean.getCertNo()) >= 2 ? "1" : "0");
        // 网贷系统ID
        qryRptsInfArry.setUsrID("2003000");
        // 0328
        qryRptsInfArry.setDeptNo(nlsCreditInfoVO.getNlsOperOrgid());
        //地址栏信息1
        qryRptsInfArry.setAddrColInf1(wsApplyNotify.getAddress());
        //证件有效期
        qryRptsInfArry.setIdentVldty(wsApplyNotify.getCertValidEndDate());
        // 征信信息
        extGuarDtlInf.setUsrSsmCrdtScor(nlsWsdExtInfo.getZM_SCORE());
        extGuarDtlInf.setRskStrtfcn(nlsWsdExtInfo.getRISK_SEG());
        extGuarDtlInf.setSlvncy(nlsWsdExtInfo.getREPAYMENT_SEG());
        extGuarDtlInf.setWhthrHvcar(nlsWsdExtInfo.getHAVE_CAR_FLAG());
        extGuarDtlInf.setWhthrHvRoom(nlsWsdExtInfo.getHAVE_FANG_FLAG());
        extGuarDtlInf.setRecntMnthQryFinInstNum(nlsWsdExtInfo.getAUTH_FIN_LAST_1M_CNT());
        extGuarDtlInf.setRecntTrplMnthQryFinInstNum(nlsWsdExtInfo.getAUTH_FIN_LAST_3M_CNT());
        extGuarDtlInf.setRecntSixMnthQryFinInstNum(nlsWsdExtInfo.getAUTH_FIN_LAST_6M_CNT());
        extGuarDtlInf.setRecntSixMnthOvdueTotNum(nlsWsdExtInfo.getOVD_ORDER_CNT_6M());
        String ovd_order_amt_6M = nlsWsdExtInfo.getOVD_ORDER_AMT_6M();
        extGuarDtlInf.setRecntSixMnthOvdueTotNAmt(Double.valueOf(StringUtils.isEmpty(nlsWsdExtInfo.getOVD_ORDER_AMT_6M()) ? "0" : ovd_order_amt_6M));
        extGuarDtlInf.setMblNoStbzDayNum(nlsWsdExtInfo.getMOBILE_FIXED_DAYS());
        extGuarDtlInf.setAddrStbzDayNum(nlsWsdExtInfo.getADR_STABILITY_DAYS());
        extGuarDtlInf.setRecntSixMnthLqdtDayAvVal(nlsWsdExtInfo.getLAST_6M_AVG_ASSET_TOTAL());
        String tot_pay_amt_6M = nlsWsdExtInfo.getTOT_PAY_AMT_6M();
        extGuarDtlInf.setRecntSixMnthPymtTotNAmt(Double.valueOf(StringUtils.isEmpty(tot_pay_amt_6M) ? "0" : tot_pay_amt_6M));
        extGuarDtlInf.setCnsmLvl(nlsWsdExtInfo.getXFDC_INDEX());
        extGuarDtlInf.setRecntSixMnthAlipTxnNum(nlsWsdExtInfo.getALI_LAST_6M_TRADE_TOTAL());
        extGuarDtlInf.setIndtCmrcRgstrnDt(wsApplyNotify.getRegisterDate());
        extGuarDtlInf.setVldIndtCmrcInfFlg(wsApplyNotify.getBusInfoExistFlag());
        extGuarDtlInf.setRgstdCptl(wsApplyNotify.getRegisterFund());
        extGuarDtlInf.setMblNo(wsApplyNotify.getMobile());
        extGuarDtlInf.setBizSt(wsApplyNotify.getStatusId());
        crdtInfo.setLoanOvdueLngstOvdueMnth(String.valueOf(creditResum.getLoanMaxOvdMonth()));
        crdtInfo.setCrdtCardOvdueMnth(String.valueOf(creditResum.getCardOvdMonth()));
        crdtInfo.setCrdtCardOvdueLngstOvdueMnth(String.valueOf(creditResum.getCardMaxOvdMonth()));
        crdtInfo.setQsCrdtCard60DayOdMnth(String.valueOf(creditResum.getPreCardOvdMonth()));
        crdtInfo.setQscrdtCardOv60Mos(String.valueOf(creditResum.getPreCardMaxOvdMonth()));
        crdtInfo.setLoanOvdueCnt(String.valueOf(creditResum.getLoanOvdCnt()));
        crdtInfo.setCrdtCardOvdueAcctCnt(String.valueOf(creditResum.getCardOvdAcCnt()));
        crdtInfo.setQscrdtCardOvOv60Days(String.valueOf(creditResum.getPreCardOvdAcCnt()));
        qryRptsInfArry.setExtGuarDtlInf(extGuarDtlInf);
        extGuarDtlInf.setCrdtInfo(crdtInfo);
        RiskManagementApproveReqBean.QryRptsInfArry[] qryRptsInfArrys = new RiskManagementApproveReqBean.QryRptsInfArry[]{qryRptsInfArry};
        riskManagementApproveReqBean.setQryRptsInfArry(qryRptsInfArrys);
        // 发送审批通知请求
        EsbInvokeWrapper esbInvokeWrapper = new EsbInvokeWrapper(riskManagementApproveService, riskManagementApproveReqBean, nlsQueueSoltComnService);
        EsbInvokerWorker esbInvokerWorker = new EsbInvokerWorker(esbInvokeWrapper);
        threadPools.submit(esbInvokerWorker);
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private NlsCreditInfoVO composeAndUpdateFianlCreditInfo(NlsWsdFinalReqBean reqWsdCreditBean) throws ESBException {
        // 更新授信申请额度信息
        NlsCreditInfoVO nlsCreditInfoVO = updateNlsCreditInfo(reqWsdCreditBean);
        // 插入授信复审信息
        insertWsApproveackNotify(reqWsdCreditBean);
        return nlsCreditInfoVO;

    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/8
     * 功能描述:插入复审内容
     * 修改日期:2019/8/8
     * 修改描述:
     */
    private void insertWsApproveackNotify(NlsWsdFinalReqBean reqWsdCreditBean) throws ESBException {
        WsApproveackNotifyVO wsApproveackNotifyVO = new WsApproveackNotifyVO();
        // 授信终审扩展信息
        NlsWsdExtInfo nlsWsdExtInfo = new NlsWsdExtInfo();
        try {
            nlsWsdExtInfo = Optional.ofNullable(reqWsdCreditBean.getNlsWsdExtInfo()).orElseGet(NlsWsdExtInfo::new);
        } catch (Exception e) {
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 附加信息格式错误!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "附加信息格式错误!!!");
        }
        wsApproveackNotifyVO.setRequestId(reqWsdCreditBean.getRqsIdmptntID());
        wsApproveackNotifyVO.setApplyNo(reqWsdCreditBean.getAdptCrdtAplNo());
        wsApproveackNotifyVO.setCertType(reqWsdCreditBean.getCertTp());
        wsApproveackNotifyVO.setCertName(reqWsdCreditBean.getCertNm());
        wsApproveackNotifyVO.setCertNo(reqWsdCreditBean.getCertNo());
        wsApproveackNotifyVO.setPlatformAccess(reqWsdCreditBean.getPassFlg());
        wsApproveackNotifyVO.setPlatformAdmit(reqWsdCreditBean.getCrdtRcmndnAmt());
        wsApproveackNotifyVO.setPlatformRateLimit(reqWsdCreditBean.getCrdtAnulIntRtLmt());
        wsApproveackNotifyVO.setPlatformRateBottom(reqWsdCreditBean.getCrdtAnulIntRtLwrLmt());
        wsApproveackNotifyVO.setBusinessModel(reqWsdCreditBean.getNtwrkMrchLoanPdVal());
        wsApproveackNotifyVO.setRiskSeg(nlsWsdExtInfo.getRISK_SEG());
        wsApproveackNotifyVO.setRepaymentSeg(nlsWsdExtInfo.getREPAYMENT_SEG());
        wsApproveackNotifyVO.setAliLast6MTradeTotal(nlsWsdExtInfo.getALI_LAST_6M_TRADE_TOTAL());
        wsApproveackNotifyVO.setZmScore(nlsWsdExtInfo.getZM_SCORE());
        wsApproveackNotifyVO.setIsZmAttentionFlag(nlsWsdExtInfo.getIS_ZM_ATTENTION_FLAG());
        wsApproveackNotifyVO.setIsZmFraudFlag(nlsWsdExtInfo.getIS_ZM_FRAUD_FLAG());
        wsApproveackNotifyVO.setHaveCarFlag(nlsWsdExtInfo.getHAVE_CAR_FLAG());
        wsApproveackNotifyVO.setHaveFangFlag(nlsWsdExtInfo.getHAVE_FANG_FLAG());
        wsApproveackNotifyVO.setAuthFinLast1MCnt(nlsWsdExtInfo.getAUTH_FIN_LAST_1M_CNT());
        wsApproveackNotifyVO.setAuthFinLast3MCnt(nlsWsdExtInfo.getAUTH_FIN_LAST_3M_CNT());
        wsApproveackNotifyVO.setAuthFinLast6MCnt(nlsWsdExtInfo.getAUTH_FIN_LAST_6M_CNT());
        wsApproveackNotifyVO.setOvdOrderCnt6M(nlsWsdExtInfo.getOVD_ORDER_CNT_6M());
        wsApproveackNotifyVO.setOvdOrderAmt6M(nlsWsdExtInfo.getOVD_ORDER_AMT_6M());
        wsApproveackNotifyVO.setMobileFixedDays(nlsWsdExtInfo.getMOBILE_FIXED_DAYS());
        wsApproveackNotifyVO.setAdrStabilityDays(nlsWsdExtInfo.getADR_STABILITY_DAYS());
        wsApproveackNotifyVO.setLast6MAvgAssetTotal(nlsWsdExtInfo.getLAST_6M_AVG_ASSET_TOTAL());
        wsApproveackNotifyVO.setTotPayAmt6M(nlsWsdExtInfo.getTOT_PAY_AMT_6M());
        wsApproveackNotifyVO.setXfdcIndex(nlsWsdExtInfo.getXFDC_INDEX());
        wsApproveackNotifyVO.setPositiveBizCnt1Y(nlsWsdExtInfo.getPOSITIVE_BIZ_CNT_1Y());
        wsApproveackNotifyVO.setPersonalCreditReport(JSON.toJSONString(nlsWsdExtInfo.getPERSONAL_CREDIT_REPORT()));
        wsApproveackNotifyVO.setInputDate(DateUtility.getCurrAppDateTimeStr());
        wsApproveackNotifyVO.setCreateUser(BaseConstant.ADMIN);
        wsApproveackNotifyVO.setCreateTime(new Date());
        wsApproveackNotifyVO.setUpdateUser(BaseConstant.ADMIN);
        wsApproveackNotifyVO.setUpdateTime(new Date());
        WsApproveackNotifyVO isHasWsApproveackNotify = wsApproveackNotifyService.queryByPk(wsApproveackNotifyVO);
        if (isHasWsApproveackNotify == null){
            int result = wsApproveackNotifyService.insertWsApproveackNotify(wsApproveackNotifyVO);
            if (result != 1) {
                logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 插入复审信息表出现异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "插入复审信息表出现异常!!!");
            }
        }else {
            isHasWsApproveackNotify.setCreateUser(BaseConstant.ADMIN);
            isHasWsApproveackNotify.setCreateTime(new Date());
            isHasWsApproveackNotify.setUpdateUser(BaseConstant.ADMIN);
            isHasWsApproveackNotify.setUpdateTime(new Date());
            int result = wsApproveackNotifyService.updateByPk(isHasWsApproveackNotify);
            if (result != 1) {
                logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 更新复审信息表出现异常!!!");
                throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新复审信息表出现异常!!!");
            }
        }
    }

    /**
     * 需求编号:2019D0910
     * 问题编号:
     * 开发人员: caoxin
     * 创建日期:2019/8/19
     * 功能描述:更新授信申请信息
     * 修改日期:2019/8/19
     * 修改描述:
     */
    private NlsCreditInfoVO updateNlsCreditInfo(NlsWsdFinalReqBean reqWsdCreditBean) throws ESBException {
        // 查询授信申请基本信息
        NlsCreditInfoVO nlsCreditInfoVO = nlsCreditInfoService.queryByLmtApplySeq(reqWsdCreditBean.getAdptCrdtAplNo());
        if (nlsCreditInfoVO == null){
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 授信复审查询不到授信申请表信息!!!");
            throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "授信复审查询不到授信申请表信息!!!");
        }
        // 查询授信初审信息
        WsApplyNotifyVO wsApplyNotifyVO = wsApplyNotifyService.queryByApplyNo(reqWsdCreditBean.getAdptCrdtAplNo());
        if (wsApplyNotifyVO == null){
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 授信复审查询不到初审申请表信息!!!");
            throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "授信复审查询不到初审申请表信息!!!");
        }
        // 判断复审与初审信息是否相同
        if (!Optional.ofNullable(nlsCreditInfoVO.getCertCode()).orElse("").equals(reqWsdCreditBean.getCertNo()) ||
                !Optional.ofNullable(nlsCreditInfoVO.getCusName()).orElse("").equals(reqWsdCreditBean.getCertNm())){
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 初审信息与复审信息不符请核查 申请人姓名和证件号码不一致!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "初审信息与复审信息不符请核查 申请人姓名和证件号码不一致!!!");
        }
        // 更新授信申请审批金额和申请金额 --单位是分 所以需要除于100
        nlsCreditInfoVO.setApplyAmt(StringUtils.isNotEmpty(reqWsdCreditBean.getCrdtRcmndnAmt()) ? new BigDecimal(reqWsdCreditBean.getCrdtRcmndnAmt()).divide(new BigDecimal("100")) : BigDecimal.ZERO);
        nlsCreditInfoVO.setApproveAmt(StringUtils.isNotEmpty(reqWsdCreditBean.getCrdtRcmndnAmt()) ? new BigDecimal(reqWsdCreditBean.getCrdtRcmndnAmt()).divide(new BigDecimal("100")) : BigDecimal.ZERO);
        int result = nlsCreditInfoService.updateAmtByApplySeq(nlsCreditInfoVO);
        if (result != 1) {
            logger.error("授信复审【REQUESTID】:+"+reqWsdCreditBean.getRqsIdmptntID()+" 【APPLYNO】:"+reqWsdCreditBean.getAdptCrdtAplNo()+" 更新授信申请表出现异常!!!");
            throw new ESBException(EsbRetCodeStandard.YWLJCLYC.getValue(), "更新授信申请表出现异常!!!");
        }
        return nlsCreditInfoVO;
    }

    public void setEsbRespServiceBean(String key, EsbRespServiceBean esbRespServiceBean) {
        redisUtil.set(ESBRESPSERVICEBEAN_KEY.getPrefix() + key, JSON.toJSONString(esbRespServiceBean));
    }

    public EsbRespServiceBean getEsbRespServiceBean(String key) {
        return JSONObject.parseObject(redisUtil.get(ESBRESPSERVICEBEAN_KEY.getPrefix() + key), EsbRespServiceBean.class);
    }

    // 日志信息
    private static final Logger logger = LoggerFactory.getLogger(NlsWsdProcessServcie.class);

    // 公共服务
    @Autowired
    @Qualifier("nlsPublicService")
    private NlsPublicService nlsPublicService;

    // 授信申请信息
    @Autowired
    @Qualifier("nlsCreditInfoService")
    private NlsCreditInfoService nlsCreditInfoService;

    // 授信初审信息
    @Autowired
    @Qualifier("wsApplyNotifyService")
    private WsApplyNotifyService wsApplyNotifyService;

    // 网贷业务过程表
    @Autowired
    @Qualifier("nlsProcessBizService")
    private NlsProcessBizService nlsProcessBizService;

    // 授信复审申请表
    @Autowired
    @Qualifier("wsApproveackNotifyService")
    private WsApproveackNotifyService wsApproveackNotifyService;

    // 授信终审通知
    @Autowired
    @Qualifier("firstApproveNoticeService")
    private FirstApproveNoticeService firstApproveNoticeService;

    // 大数据审批接口
    @Autowired
    @Qualifier("riskManagementApproveService")
    private RiskManagementApproveService riskManagementApproveService;

    // 额度信息
    @Autowired
    @Qualifier("lmtPrdContService")
    private LmtPrdContService lmtPrdContService;

    // 告警服务
    @Autowired
    @Qualifier("nlsQueueSoltComnService")
    private NlsQueueSoltComnService nlsQueueSoltComnService;

    // redis服务
    @Autowired
    private RedisUtil redisUtil;

    // 线程池
    private static ExecutorService threadPools = Executors.newCachedThreadPool();

}
